package com.bing.task;

import com.bing.entity.TUrl;
import com.bing.services.impl.TUService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by kaibb on 2016/6/1.
 */
public class GetService {
    private static final Logger LOGGER = LoggerFactory.getLogger(GetService.class);
    private static ExecutorService service = Executors.newFixedThreadPool(10);
    private volatile static boolean flag = true;
    private volatile static int total = 1;
    private volatile static int fetching = 0;

    public static Set<String> get(Set<String> urls) throws ExecutionException, InterruptedException {
        total = urls.size();
        fetching = 0;
        flag = true;
        Set<String> https = new HashSet<>();
        List<Future<Set<String>>> futures;
        Set<Fetch> subTask = new HashSet<>();
        for (String url : urls) {
            if (!flag) {
                LOGGER.info("停止抓取.....");
                break;
            }
            subTask.add(new Fetch(url));
            fetching++;
            if (fetching % 10 == 0) {
                LOGGER.debug("parsing.....");
                futures = service.invokeAll(subTask);
                for (Future<Set<String>> future : futures) {
                    final Set<String> c = future.get();
//                    LOGGER.info("抓取:{}", c);
                    https.addAll(c);
                }
                subTask.clear();
            }
        }
        futures = service.invokeAll(subTask);
        for (Future<Set<String>> future : futures) {
            https.addAll(future.get());
        }
        return https;
    }

    public static void stop() {
        flag = false;
        TUService.clear();
    }

    public static int getProcess() {
        return fetching * 100 / total;
    }
}
